package servlet;

import dao.DeptMajorDao;
import dao.OtherDao;
import net.sf.json.JSONArray;
import util.JsonWrapper;
import util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 其他的servlet，如短信验证、邮箱验证等
 *
 * @author 邓晴予
 */
@WebServlet(name = "OtherServlet", urlPatterns = {"/other", "/depts", "/majors"})
public class OtherServlet extends HttpServlet {
    private OtherDao otherDao = new OtherDao();
    private DeptMajorDao deptMajorDao = new DeptMajorDao();

    /**
     * 短信验证码发送、邮箱验证发送
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     * @author 邓晴予
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        JsonWrapper result = new JsonWrapper();

        if ("sendMessage".equals(type)) {
//          发送短信验证码
            String phone = req.getParameter("phone");
            try {
                otherDao.sendMessage(phone, req, resp);
                result.setStatus(200);
                result.setMsg("success");
            } catch (Exception e) {
                result.setStatus(403);
                result.setMsg("failed");
            }
        } else if ("sendEmail".equals(type)) {
//          发送邮箱验证码
            String email = req.getParameter("email");
            try {
                otherDao.sendEmail(email, req, resp);
                result.setStatus(200);
                result.setMsg("success");
            } catch (Exception e) {
                result.setStatus(403);
                result.setMsg("failed");
            }
            ResponseUtil.write(resp, result);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if ("/depts".equals(req.getRequestURI())) {
            List<Map<String, String>> r = deptMajorDao.getDepartments();
            JSONArray array = JSONArray.fromObject(r);
            ResponseUtil.write(resp, JsonWrapper.builder().setStatus(200).setMsg("success").setDataJsonArray("depts", array).build());
        } else if ("/majors".equals(req.getRequestURI())) {
            String deptId = req.getParameter("deptId");
            if (deptId == null) {
                ResponseUtil.writeBadRequest(resp);
            } else {
                List<Map<String, String>> r = deptMajorDao.getMajors(deptId);
                JSONArray array = JSONArray.fromObject(r);
                ResponseUtil.write(resp, JsonWrapper.builder().setStatus(200).setMsg("success").setDataJsonArray("majors", array).build());
            }
        } else {
            ResponseUtil.writeBadRequest(resp);
        }
    }
}
